package br.com.universidade.dao;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import br.com.universidade.util.HibernateUtil;

 abstract class GenericDAO<T> {

	public void save(T objeto) throws Exception{
		HibernateUtil factory = new HibernateUtil();
		Session sessao =factory.getSession();
		sessao.save(objeto);
		sessao.beginTransaction().commit();
	}
	
	public void deletar(T objeto) throws Exception{
		HibernateUtil factory = new HibernateUtil();
		Session sessao =factory.getSession();
		sessao.delete(objeto);
		sessao.beginTransaction().commit();
	}
	
	public void update(T objeto) throws Exception{
		HibernateUtil factory = new HibernateUtil();
		Session sessao =factory.getSession();
		sessao.update(objeto);
		sessao.beginTransaction().commit();
	}
	
	@SuppressWarnings("unchecked")
	public T getById(int  idObjeto, Class classe) throws Exception{
		HibernateUtil factory = new HibernateUtil();
		Session sessao =factory.getSession();
		return (T) sessao.get(classe, idObjeto);
	}
	
	@SuppressWarnings("unchecked")
	public List<T> findAll(Class classe) throws Exception{
		HibernateUtil factory = new HibernateUtil();
		Session sessao =factory.getSession();
		Query query = sessao.createQuery("select o from "+classe.getSimpleName()+" o ");
		return query.list();
		
	}
	
}
